#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <vector>
#include <string>
#include <stdexcept>
#include <bits/stdc++.h>
#include <nlohmann/json.hpp>

using namespace std;
using json = nlohmann::json;

long double stringToLongDouble(const std::string& str) {
    long double result;
    try {
        result = std::stold(str);
    } catch (const std::invalid_argument& ia) {
        std::cerr << "Invalid argument: " << ia.what() << '\n';
        return 0.0L; // 返回0.0L或适当的错误值
    } catch (const std::out_of_range& oor) {
        std::cerr << "Out of range: " << oor.what() << '\n';
        return std::numeric_limits<long double>::infinity()
                   * (str[0] == '-' ? -1.0L : 1.0L); // 返回无穷大或负无穷大
    }
    return result;
}
int main() {
    std::ifstream file("/home/dmf/test/ttzzbb/output_22km.csv"); // 打开文件
    if (!file.is_open()) {
        std::cerr << "Unable to open file." << std::endl;
        return 1;
    }
    std::string line;
    map<string, vector<string>> dataMap; // 创建map来存储数据
    map<string,int> num;
    while (std::getline(file, line)) { // 逐行读取
        std::istringstream s(line);
        std::string cell;
        std::vector<std::string> row;

        // 按逗号分割每行
        while (std::getline(s, cell, ',')) {
            row.push_back(cell);
        }

        // 将第一列作为键，其余列作为值
        if (!row.empty()) {
            dataMap[row[0]] = row; // 将整行作为值存储，或者你可以只存储除了第一列之外的部分
        }
    }
    
    for (const auto& pair : dataMap) {
        std::cout << " Key: " << pair.first << ", Values: ";
        for (const auto& value : pair.second) {
            std::cout << value << " ";
        }
        std::cout << std::endl;
    }
    
    file.close(); // 关闭文件

    std::ifstream id_file("/home/dmf/test/ttzzbb/satellite_new.csv"); // 打开文件
    if (!id_file.is_open()) {
        std::cerr << "Unable to open file." << std::endl;
        return 1;
    }
    vector<string> id;
    while (std::getline(id_file, line)) { // 逐行读取
        std::istringstream s(line);
        std::string cell;
        std::vector<std::string> row;

        // 按逗号分割每行
        while (std::getline(s, cell, ',')) {
            row.push_back(cell);
        }

        // 将第一列作为键，其余列作为值
        if (!row.empty() && row[0]!= "satellite_id") {
            id.emplace_back(row[0]);
        }
    }
    id_file.close(); // 关闭文件

   for(int i = 0; i < id.size(); i++){
        json j;
        map<string,vector<long double>> newinfo;
        vector<long double> n1;
        vector<long double> n2;
        string i1 =  "satellite_id";
        string i2 =  "point_longitudes";
        string i3 =  "point_latitudes";

        j[i1] = id[i];
        for (const auto& pair : dataMap) {
            n1.emplace_back(stringToLongDouble(pair.second[1]));
            n2.emplace_back(stringToLongDouble(pair.second[2]));
        } 

    // 将JSON数据写入到一个新的文件中
        string outfilepath = "/home/dmf/test/ttzzbb/mjson2/";
        string outfileid = id[i];
        string outfilename = outfilepath + outfileid + ".json";
        std::ofstream outFile(outfilename);
        if (!outFile.is_open()) {
            std::cerr << "Error opening output file." << std::endl;
            return 1;
        }
        outFile << "{" << "\n";
        outFile << "    " <<"\"satellite_id\": \"" << id[i] << "\",\n";
        outFile << "    " << "\"point_longitudes\": [";
        outFile << n2[0];
        for(int i=1;i<n1.size();i++){
            outFile << ", " << n2[i];
        }
        outFile << "],\n";
        outFile << "    " << "\"point_latitudes\": [";
        outFile << n1[0];
        for(int i=1;i<n2.size();i++){
            outFile << ", " << n1[i];
        }
        outFile << "]\n";
        outFile << "}" << "\n";
        //outFile << j.dump(4); // 使用4个空格进行缩进美化输出
        outFile.close();

   }

    return 0;
}